package com.flower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flower.domain.dto.buyer.address.AddressDto;
import com.flower.domain.entity.Address;
import com.flower.mapper.AddressMapper;
import com.flower.service.AddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Override
    public List<Address> getAddressList(Long buyerId) {
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getBuyerId, buyerId)
               .orderByDesc(Address::getIsDefault);
        return this.list(wrapper);
    }

    @Override
    public void addAddress(AddressDto addressDto) {
        Address address = new Address();
        BeanUtils.copyProperties(addressDto, address);
        
        // 如果是默认地址，需要将其他地址设为非默认
        if (Boolean.TRUE.equals(addressDto.getIsDefault())) {
            clearOtherDefault(Long.valueOf(addressDto.getBuyerId()));
            address.setIsDefault(1);
        } else {
            address.setIsDefault(0);
        }
        
        this.save(address);
    }

    @Override
    public void updateAddress(AddressDto addressDto) {
        Address address = new Address();
        BeanUtils.copyProperties(addressDto, address);
        
        // 验证地址归属
        checkAddressOwnership(address.getAddressId(), Long.valueOf(addressDto.getBuyerId()));
        
        // 如果是默认地址，需要将其他地址设为非默认
        if (Boolean.TRUE.equals(addressDto.getIsDefault())) {
            clearOtherDefault(Long.valueOf(addressDto.getBuyerId()));
            address.setIsDefault(1);
        } else {
            address.setIsDefault(0);
        }
        
        this.updateById(address);
    }

    @Override
    public void deleteAddress(Integer addressId, Long buyerId) {
        // 验证地址归属
        checkAddressOwnership(addressId, buyerId);
        this.removeById(addressId);
    }

    @Override
    @Transactional
    public void setDefaultAddress(Integer addressId, Long buyerId) {
        // 验证地址归属
        checkAddressOwnership(addressId, buyerId);

        // 清除其他默认地址
        clearOtherDefault(buyerId);
        
        // 设置新的默认地址
        Address address = this.getById(addressId);
        address.setIsDefault(1);
        this.updateById(address);
    }

    // 清除其他默认地址
    private void clearOtherDefault(Long buyerId) {
        // 清除其他默认地址
        lambdaUpdate().eq(Address::getBuyerId, buyerId.intValue())
                .eq(Address::getIsDefault, 1)
                .set(Address::getIsDefault, 0)
                .update();
    }
    // 验证地址归属
    private void checkAddressOwnership(Integer addressId, Long buyerId) {
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getAddressId, addressId)
               .eq(Address::getBuyerId, buyerId.intValue());

        if (this.count(wrapper) == 0) {
            throw new RuntimeException("无权操作此地址");
        }
    }
} 